﻿/* 深度图在Unity实现参考自：
 * https://blog.csdn.net/ylbs110/article/details/53366112
 * 有趣深度图
 * https://www.cnblogs.com/murongxiaopifu/p/7050233.html
 */
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[ExecuteInEditMode]
/// <summary>
/// dmp == DepthMapPRoject
/// </summary>
public class DMPDepthMap : MonoBehaviour
{
    [Header("应挂在 MainCam ")]
    public Shader depthShader;
    public float cameraFarClipPlan=15f;//近的摄像机
    [Header("自动赋值(修改Shader后需手动清一下）")]
    public Material depthMat;
    public bool isUseNullMat;
    public void OnEnable()
    {
        Camera cam = GetComponent<Camera>();
        cam.depthTextureMode = DepthTextureMode.Depth;
        cam.farClipPlane = cameraFarClipPlan;
        InitMat();

    }
    void InitMat() {
        if (depthShader == null) return;
        if (depthMat == null)
            depthMat = new Material(depthShader);

        if (depthMat.shader != depthShader)
        {
            depthMat.shader = depthShader;
        }
    }
    private void OnRenderImage(RenderTexture src, RenderTexture dest)
    {
        InitMat();
        if (isUseNullMat)
            Graphics.Blit(src, dest);
        else
            Graphics.Blit(src, dest, depthMat);
    }
}
